home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr53
/
pctv4n_1.zip
/
CHILD.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-10
|
6KB
|
202 lines
// child.cpp -- TChildWindow implementation
#include "child.h"
// Return maximum of a or b
inline long max(long a, long b)
{
return (a > b) ? a : b;
}
// Return minimum of a or b
inline long min(long a, long b)
{
return (a < b) ? a : b;
}
// Construct the main window and add scrollers
TChildWindow::TChildWindow(PTWindowsObject AParent, LPSTR fName)
: text(250, 0, 125), TWindow(AParent, fName)
{
Attr.Style |= WS_VSCROLL | WS_HSCROLL;
strcpy(fileName, fName);
fontColor = GetSysColor(COLOR_WINDOWTEXT);
fontPointSize = 10;
numRows = numCols = 0;
width = height = 0;
glyphH = glyphW = 0;
colSep = rowSep = 0;
Scroller = new TScroller(this, 1, 1, 0, 0);
if (Scroller) Scroller->AutoOrg = FALSE;
if (!ReadFile()) {
MessageBox(HWindow, fileName, "Error opening file",
MB_ICONSTOP | MB_OK);
Status = EM_INVALIDCHILD;
}
}
void TChildWindow::SetupWindow()
{
int logPixelsY; // Number of pixels per vertical logical inch
HDC hDC; // Device context for obtaining system specs
hDC = GetDC(HWindow);
logPixelsY = GetDeviceCaps(hDC, LOGPIXELSY);
ReleaseDC(HWindow, hDC);
memset(&font, 0, sizeof(font));
font.lfHeight = -MulDiv(fontPointSize, logPixelsY, 72);
font.lfWeight = FW_NORMAL;
font.lfCharSet = ANSI_CHARSET;
font.lfOutPrecision = OUT_TT_PRECIS;
font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
font.lfQuality = PROOF_QUALITY;
font.lfPitchAndFamily = FIXED_PITCH;
strcpy(font.lfFaceName, "Courier New");
hFont = CreateFontIndirect(&font);
if (hFont == NULL)
Status = EM_INVALIDCHILD;
AdjustSettings();
TWindow::SetupWindow();
}
// Read strings from text file into String object array
int TChildWindow::ReadFile()
{
FILE *f; // DOS file pointer
char buffer[BUFSIZE]; // Input buffer
long len; // Length of string in buffer
char *cp; // Miscellaneous character pointer
String *sp; // Pointer to a String object
f = fopen(fileName, "r");
if (!f) return FALSE;
text.flush();
numRows = numCols = 0;
while (fgets(buffer, BUFSIZE, f)) {
cp = strchr(buffer, '\n');
if (cp)
*cp = NULL;
sp = new String(buffer);
len = (long)strlen(buffer);
if (len > numCols)
numCols = len;
text.add(*sp);
numRows++;
}
fclose(f);
return TRUE;
}
// Adjust document and font settings
void TChildWindow::AdjustSettings()
{
TEXTMETRIC tm; // Text (font) information
HDC hDC; // Handle to device context
HFONT hOldFont; // Handle to old hDC font
if (hFont == NULL) return;
hDC = GetDC(HWindow);
hOldFont = (HFONT)SelectObject(hDC, hFont);
GetTextMetrics(hDC, &tm);
glyphW = tm.tmMaxCharWidth;
glyphH = tm.tmHeight;
rowSep = tm.tmExternalLeading;
colSep = GetTextCharacterExtra(hDC);
SelectObject(hDC, hOldFont);
width = colSep + ((numCols + 1) * (long)(glyphW + colSep));
height = rowSep + ((numRows + 1) * (long)(glyphH + rowSep));
ReleaseDC(HWindow, hDC);
}
// Adjust scroll bars to match document and font settings
void TChildWindow::AdjustScroller()
{
RECT R; // Rectangle for client area dimensions
long H, W; // Document size minus window in display units
if (numRows <= 0) return;
GetClientRect(HWindow, &R);
W = width - (R.right - R.left);
H = height - (R.bottom - R.top);
Scroller->SetUnits(glyphW + colSep, glyphH + rowSep);
Scroller->SetRange(W / Scroller->XUnit, H / Scroller->YUnit);
SetScrollPos(HWindow, SB_HORZ, Scroller->XPos, TRUE);
SetScrollPos(HWindow, SB_VERT, Scroller->YPos, TRUE);
}
// Select font, size, and style
void TChildWindow::CMFontSelect(RTMessage)
{
CHOOSEFONT cf; // Choose-font dialog structure
LOGFONT tempFont; // Temporary copy of TFontWin's logical font
memset(&cf, 0, sizeof(cf));
tempFont = font;
cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = HWindow;
cf.Flags = CF_FIXEDPITCHONLY | CF_EFFECTS |
CF_INITTOLOGFONTSTRUCT | CF_BOTH;
cf.lpLogFont = &tempFont;
cf.rgbColors = fontColor;
if (ChooseFont(&cf)) {
font = *cf.lpLogFont;
fontColor = cf.rgbColors;
fontPointSize = cf.iPointSize / 10;
if (hFont) DeleteObject(hFont);
hFont = CreateFontIndirect(&font);
AdjustSettings();
AdjustScroller();
InvalidateRect(HWindow, NULL, TRUE);
UpdateWindow(HWindow);
}
}
// Respond to changes in the child window's size
void TChildWindow::WMSize(RTMessage Msg)
{
TWindow::WMSize(Msg);
if (Scroller && Msg.WParam != SIZEICONIC)
AdjustScroller();
}
// Respond to WM_KEYDOWN messages directed to child
void TChildWindow::WMKeyDown(RTMessage msg)
{
long XLine = Scroller->XLine;
long YLine = Scroller->YLine;
long YPage = Scroller->YPage;
switch (msg.WParam){
case VK_UP: Scroller->ScrollBy(0, -YLine ); break;
case VK_DOWN: Scroller->ScrollBy(0, YLine ); break;
case VK_LEFT: Scroller->ScrollBy(-XLine, 0 ); break;
case VK_RIGHT: Scroller->ScrollBy( XLine, 0 ); break;
case VK_PRIOR: Scroller->ScrollBy(0, -YPage ); break;
case VK_NEXT: Scroller->ScrollBy(0, YPage ); break;
default: return;
}
msg.Result = 0;
}
// Paint or repaint the child (document) window contents
void TChildWindow::Paint(HDC PaintDC, PAINTSTRUCT &ps)
{
int x, y; // String coordinates
int row, col; // Row and column numbers
int startRow, endRow; // Rows in update region
String *sp; // String object pointer
HFONT hOldFont; // Old font handle
if (numRows <= 0) return;
SetTextColor(PaintDC, fontColor);
SetBkColor(PaintDC, GetSysColor(COLOR_WINDOW));
hOldFont = (HFONT)SelectObject(PaintDC, hFont);
startRow = (int)max(0,
1 + (Scroller->YPos + ps.rcPaint.top / Scroller->YUnit - 1));
endRow = (int)min(numRows,
1 + (Scroller->YPos + ps.rcPaint.bottom / Scroller->YUnit));
for (row = startRow; row < endRow; row++) {
x = colSep + ((Scroller->XUnit * (1 - Scroller->XPos))
- Scroller->XUnit);
y = rowSep + ((Scroller->YUnit * (1 - Scroller->YPos + row))
- Scroller->YUnit);
sp = (String *)&text[row];
TextOut(PaintDC, x, y, *sp, strlen(*sp));
}
SelectObject(PaintDC, hOldFont);
}